<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8" />
    <title>Answer</title>
    <script
      crossorigin
      src="https://unpkg.com/react@16/umd/react.development.js"
    ></script>
    <script
      crossorigin
      src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"
    ></script>
    <script
      crossorigin
      src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"
    ></script>
    <script src="https://cdn.jsdelivr.net/npm/babel-regenerator-runtime@6.5.0/runtime.min.js"></script>
  </head>
  <body>
    <div class="main">
      <div class="section" id="app1"></div>
    </div>
    <script type="text/babel">
      let rtc;

      const App = () => {
        React.useEffect(() => {
          (async () => {
            const socket = new WebSocket("ws://localhost:8888");
            await new Promise((r) => (socket.onopen = r));
            console.log("open websocket");

            const peer = (rtc = new RTCPeerConnection({
              iceServers: [],
            }));
            peer.onicecandidate = ({ candidate }) => {
              if (!candidate) {
                const sdp = JSON.stringify(peer.localDescription);
                socket.send(sdp);
              }
            };
            peer.ontrack = ({ streams }) => {
              const video = document.createElement("video");
              video.srcObject = streams[0];
              video.style = "max-width:500px";
              video.play();
              document.body.appendChild(video);
            };

            const offer = await new Promise(
              (r) => (socket.onmessage = (ev) => r(JSON.parse(ev.data)))
            );
            (
              await navigator.mediaDevices.getUserMedia({
                video: { width: 1920 },
              })
            )
              .getVideoTracks()
              .forEach((track) => {
                const t = peer.addTrack(track);
                setTimeout(() => {
                  const params = t.getParameters();
                  params.encodings = [{ maxBitrate: 45 * 1000 * 1000 }];
                  t.setParameters(params);
                }, 1000);
              });
            (
              await navigator.mediaDevices.getUserMedia({
                video: { width: 1920 },
              })
            )
              .getVideoTracks()
              .forEach((track) => {
                const t = peer.addTrack(track);
                setTimeout(() => {
                  const params = t.getParameters();
                  params.encodings = [{ maxBitrate: 45 * 1000 * 1000 }];
                  t.setParameters(params);
                }, 1000);
              });
            await peer.setRemoteDescription(offer);
            const answer = await peer.createAnswer();
            await peer.setLocalDescription(answer);
          })();
        }, []);

        return (
          <div>
            answer
            <div style={{ display: "flex" }}></div>
          </div>
        );
      };

      ReactDOM.render(<App />, document.getElementById("app1"));
    </script>
  </body>
</html>
